$(document).ready(function(){
    /***
     *  mainBanner  begin
     ***/
    var mainBanner = '';
    //获取mainBanner列表函数
    function getBannerList(){
        getAjax(`${global.apiUrl}/get_community_health_banner`,function (res) {
            console.log(res);
            var html = '';
            mainBanner = res;
            for(var i in res){
                html += `
                 <div class="banner-item">
                    <img src="${res[i].banner_img}" alt="" >
                    <div class="eventBox themeColor">
                        <div class="reset" data-toggle="modal" data-target="#myModal" data-id="${res[i].id}" data-type="2">修改</div>
                        <div class="delete" data-id="${res[i].id}" >删除</div>
                    </div>
                 </div>
            `
            }
            html += `
             <div class="banner-add" data-toggle="modal" data-target="#myModal" data-type="1">
                <img src="../../img/community-service/banner_add.png" alt="">
            </div>
        `
            $('#banner-box').html(html);
        },{
            community_id: global.community_id
        })
    }
    //获取mainBanner
    getBannerList();
    //mainBanner修改删除按钮
    $('#banner-box').on('mouseenter','.banner-item',function(){
        $(this).find('.eventBox').css('display','block');
        $(this).find('.eventBox').css('opacity','1');
        // $('.eventBox').css('display','block');
        // $('.eventBox').css('opacity','1');
    })
    $('#banner-box').on('mouseleave','.banner-item',function(){
        $(this).find('.eventBox').css('opacity','.3');
        $(this).find('.eventBox').css('display','none');
    })
    //模态框显示触发事件（添加  修改）
    $('#myModal').on('show.bs.modal',function(event){
        var btnThis = $(event.relatedTarget);
        var modal = $(this);
        // modal.find('.preview-img')
        modal.find('#imgUrl').val('');
        modal.find('.preview-img').html(`<div>图片预览</div>`);
        var type = btnThis.data('type');
        // console.log(type);
        if(type == 1){
            modal.find('#myModalLabel').text('添加BANNER');
            modal.find('#uploadBannerImg').attr('data-type','1');
            modal.find('#bannerId').val('');
            modal.find('#banner_status').val(-1)
        }
        else if(type == 2){
            modal.find('#myModalLabel').text('修改BANNER');
            modal.find('#uploadBannerImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id,mainBanner);
            modal.find('#bannerId').val(id);
            for(var i in mainBanner){
                if(id == mainBanner[i].id){
                    modal.find('.preview-img').html(`<img src="${mainBanner[i].banner_img}" >`)
                    modal.find('#banner_status').val(mainBanner[i].url_id?mainBanner[i].url_id:-1)
                }
            }
        }
    })
    //删除
    $('#banner-box').on('click','.delete',function(){
        var id = $(this).data('id');
        var r=confirm("是否要删除该轮播图？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_health_banner`,function(res){
                // console.log(res);
                if(res == 1){
                    getBannerList();
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }
    })
    //设置imgUrl值到隐藏的input
    function setImgUrl(res){
        // console.log(res);
        document.getElementById('imgUrl').value = res;
    }
    //上传图片获取图片地址
    imgUpload('fileInput','fileImage',setImgUrl);
    //上传banner图片
    $('#uploadBannerImg').on('click',function(){
        var $imgUrl = $('#imgUrl').val();
        var $bannerId = $('#bannerId').val();
        var type = $('#uploadBannerImg').data('type');
        var urlId = $('#banner_status').val();
        console.log(urlId)
        // console.log($imgUrl,$bannerId);
        // console.log(type);
        if($imgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            getAjax(`${global.apiUrl}/admin_operation_community_health_banner`,function(res){
                if(res == 1){
                    getBannerList();
                    alert('上传成功！');
                    $('#myModal').modal('hide');
                }
            },{
                type:type,
                id:$bannerId,
                url_id: urlId,
                banner_img:$imgUrl,
                community_id: global.community_id
            })
        }
    })
    /***
     *  mainBanner  end
     ***/

    //导航切换
    $('.nav-item').on('click',function(){
        $('.nav-item').removeClass('active');
        $(this).addClass('active');
        var index = $(this).index();
        // console.log(index);
        $('.item-box').css('display','none');
        var itemBox = document.getElementsByClassName('item-box');
        itemBox[index].style.display = 'block';
    })


    /***
    *  健康商城部分 begin
    ***/

    /*** banner ***/
    var homemakingBannerList = ''
    //健康商城服务banner
    function getHomemakingBannerList(){
        getAjax(`${global.apiUrl}/get_community_health_shop_banner`,function(res){
            // console.log(res);
            var html = '';
            homemakingBannerList = res;
            for(var i in res){
                html += `
                 <div class="banner-item">
                    <img src="${res[i].banner_img}" alt="" >
                    <div class="eventBox themeColor">
                        <div class="reset" data-toggle="modal" data-target="#homemakingBannerModal" data-id="${res[i].id}" data-type="2">修改</div>
                        <div class="delete" data-id="${res[i].id}" >删除</div>
                    </div>
                 </div>
            `
            }
            html += `
             <div class="banner-add" data-toggle="modal" data-target="#homemakingBannerModal" data-type="1">
                <img src="../../img/community-service/banner_add.png" alt="">
            </div>
        `
            $('#banner-item-box').html(html);
        },{
            community_id: global.community_id
        })
    }
    getHomemakingBannerList();
    $('#banner-item-box').on('mouseenter','.banner-item',function(){
        $(this).find('.eventBox').css('display','block');
        $(this).find('.eventBox').css('opacity','1');
    })
    $('#banner-item-box').on('mouseleave','.banner-item',function(){
        $(this).find('.eventBox').css('opacity','.3');
        $(this).find('.eventBox').css('display','none');
    })
    //modal 触发同时
    $('#homemakingBannerModal').on('show.bs.modal',function(event){
        var btnThis = $(event.relatedTarget);
        var modal = $(this);
        // modal.find('.preview-img')
        modal.find('#homemakingImgUrl').val('');
        modal.find('.preview-img').html(`<div>图片预览</div>`);
        var type = btnThis.data('type');
        // console.log(type);
        if(type == 1){
            modal.find('#homingmakingModalLabel').text('添加BANNER');
            modal.find('#uploadHomemakingBannerImg').attr('data-type','1');
            modal.find('#homemakingBannerId').val('');
        }
        else if(type == 2){
            modal.find('#homingmakingModalLabel').text('修改BANNER');
            modal.find('#uploadHomemakingBannerImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id,mainBanner);
            modal.find('#homemakingBannerId').val(id);
            // console.log(homemakingBannerList);
            for(var i in homemakingBannerList){
                if(id == homemakingBannerList[i].id){
                    modal.find('.preview-img').html(`<img src="${homemakingBannerList[i].banner_img}" >`)
                }
            }
        }
    });
    function setHomemakingImgUrl(res){
        document.getElementById('homemakingImgUrl').value = res;
    }
    imgUpload('homemakingInput','homemakingImage',setHomemakingImgUrl);
    //上传，修改
    $('#uploadHomemakingBannerImg').on('click',function(){
        var $imgUrl = $('#homemakingImgUrl').val();
        var $bannerId = $('#homemakingBannerId').val();
        var type = $('#uploadHomemakingBannerImg').data('type');
        // console.log($imgUrl,$bannerId);
        // console.log(type);
        if($imgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            getAjax(`${global.apiUrl}/admin_operation_community_health_shop_banner`,function(res){
                if(res == 1){
                    getHomemakingBannerList();
                    alert('上传成功！');
                    $('#homemakingBannerModal').modal('hide');
                }
            },{
                type:type,
                id:$bannerId,
                banner_img:$imgUrl,
                community_id: global.community_id
            })
        }
    })
    //删除
    $('#banner-item-box').on('click','.delete',function(){
        var id = $(this).data('id');
        var r = confirm("是否要删除该轮播图？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_health_shop_banner`,function(res){
                if(res == 1){
                    getHomemakingBannerList();
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }

    })

    /***  分类 begin   ***/
    var homemakingClass = '';
    function getHomemakingClass(){
        getAjax(`${global.apiUrl}/get_community_health_shop_product_class`,function(res){
            // console.log(res);
            var html = '';
            var addBusinessClass = '';
            var selHtml = '<option value="0">全部</option>';
            homemakingClass = res;
            for(var i in res){
                html += `
                    <div class="class-item">
                    <div class="icon-box">
                        <img src="../../img/community-service/shop.png" alt="">
                    </div>
                    <div class="icon-text">${res[i].class_name}</div>
                    <div class="class-reset themeColor" data-toggle="modal" data-target="#classModal" data-type="2" data-id="${res[i].id}">修改</div>
                    <div class="class-delete themeColor" data-id="${res[i].id}">删除</div>
                </div>
                `
                selHtml += `
                     <option value="${res[i].id}">${res[i].class_name}</option>
                     
                `
                addBusinessClass += `
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" class="class-checked" value="${res[i].id}"> ${res[i].class_name}
                        </label>
                    </div>
                `
            }
            html += `
                <div class="class-item-add" data-toggle="modal" data-target="#classModal" data-type="1">
                    <div class="icon-box-add">
                        <img src="../../img/community-service/banner_add.png" alt="">
                    </div>
                    <div class="icon-text-add">添加</div>
                </div>
            `
            $('#homemakingClass').html(html);
            $('#homemaking-class-sel ').html(selHtml);
            $('#add-shop-class-box').html(addBusinessClass);

        },{
            community_id: global.community_id
        })
    }
    getHomemakingClass();
    $('.class-box').on('mouseenter','.class-item',function(){
        // console.log(1);
        $(this).css('boxShadow','0 0 8px #f1f1f1');
        $(this).find('.class-reset').css('display','block');
        $(this).find('.class-delete').css('display','block');
    })
    $('.class-box').on('mouseleave','.class-item',function(){
        $(this).css('boxShadow','');
        $(this).find('.class-reset').css('display','none');
        $(this).find('.class-delete').css('display','none');
    })
    $('#classModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        // console.log(type);
        modal.find('#className').val('');
        modal.find('.preview-class-img').html(`<div>图片预览</div>`);
        if(type == 1){
            modal.find('#classModalLabel').text('添加分类');
            modal.find('#uploadClassImg').attr('data-type','1');
            modal.find('#classItemId').val('');
        }else if(type == 2){
            modal.find('#classModalLabel').text('修改分类');
            modal.find('#uploadClassImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id);
            // console.log(id,mainBanner);
            modal.find('#classItemId').val(id);
            // console.log(homemakingClass);
            for(var i in homemakingClass){
                if(id == homemakingClass[i].id){
                    modal.find('.preview-class-img').html(`<img src="${homemakingClass[i].url}" >`);
                    modal.find('#className').val(homemakingClass[i].class_name);
                }
            }
        }
    })
    function setClassImgUrl(res){
        document.getElementById('classImgUrl').value = res;
    }
    //上传图片获取图片地址
    // imgUpload('classInput','classImage',setClassImgUrl);
    $('#uploadClassImg').on('click',function(){
        var $classImgUrl = $('#classImgUrl').val();
        var $className = $('#className').val();
        var $classId = $('#classItemId').val();
        var type = $('#uploadClassImg').data('type');
        // console.log($classImgUrl,$classId,$className);
        // console.log(type);
        if($classImgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            getAjax(`${global.apiUrl}/admin_operation_community_health_shop_product_class`,function(res){
                if(res == 1){
                    getHomemakingClass();
                    alert('上传成功！');
                    $('#classModal').modal('hide');
                }
            },{
                type:type,
                id:$classId,
                url:$classImgUrl,
                class_name:$className,
                community_id: global.community_id
            })
        }
    })
    $('#homemakingClass').on('click','.class-delete',function(){
        var id = $(this).data('id');
        // console.log(id);
        var f = confirm('删除分类可能造成！软件异常，是否确实删除');
        if(f){
            getAjax(`${global.apiUrl}/admin_operation_community_health_shop_product_class`,function(res){
                // console.log(res);
                if(res == 1){
                   getHomemakingClass();
                //    console.log(res);
                   alert('删除成功！')
               }else if(res == 2){
                   alert('该分类存在商家！不可删除！');
               }

            },{
                type:3,
                id:id
            })
        }
    })

    /***  商户列表  ***/
    //获取商户列表
    var homemakingPageNo = 1,showNum=10;
    var homemakingList = '';
    function getHomemakingBusiness(p,showNum){
        getAjax(`${global.apiUrl}/admin_get_community_health_shop`,function(res){
            console.log(res)
            if(res.list.length == 0){
                $("#page").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getHomemakingBusiness(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无商家</td>
                        </tr>`
                $('#homemaking-business').html(html);
            }else{
                $("#page").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.all/showNum), //总页数
                    totalSize: res.all,
                    callback: function(num) {
                        getHomemakingBusiness(num,showNum)
                        homemakingPageNo = num
                    }
                });
                var html = '';
                homemakingList = res.list;
                var list = res.list;
                var className = '';
                for(var i in list){
                    html += `
                        <tr>
                            <td class="shop-img">
                                <img src="${list[i].shop_img}" alt="">
                            </td>
                            <td>${list[i].shop_name}</td>
                            <td>${list[i].shop_phone}</td>
                            <td>${list[i].address}</td>
                            <td class="control-td themeColor">
                                <span class="show2" data-toggle="modal" data-target="#BusinessInfoModal" data-id="${list[i].id}">查看信息</span>
                                <span class="orders" data-id="${list[i].id}" data-name="${list[i].shop_name}" style="margin-left: 15px">查看订单</span> 
                                <span class="reset" data-id="${list[i].id}" data-toggle="modal" data-target="#BusinessResetModal" style="margin-left: 15px">重置密码</span>
                            </td>
                        </tr>
                `
                }
                $('#homemaking-business').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id:global.community_id
        })
    }
    getHomemakingBusiness(homemakingPageNo,showNum);

    $('#BusinessModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var account = modal.find('#account');
        account.val('');
        var password = modal.find('#password');
        password.val('');
        var rebate = modal.find('#rebate');
        rebate.val('')
        var rebate2 = modal.find('#rebate2');
        rebate2.val('')
        var rebate3 = modal.find('#rebate3');
        rebate3.val('')
        var shopName = modal.find('#shopName');
        shopName.val('');
        var shopPhone = modal.find('#shopPhone');
        shopPhone.val('');
    })

    $('#BusinessInfoModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);        
        var account = modal.find('#account-show');
        account.val('');
        var rebate = modal.find('#rebate-show');
        rebate.val('')
        var rebate2 = modal.find('#rebate-show2');
        rebate2.val('')
        var rebate3 = modal.find('#rebate-show3');
        rebate3.val('')
        var city = modal.find('#city');
        city.val('')
        var shopName = modal.find('#shopName-show');
        shopName.val('');
        var phone = modal.find("#shopPhone-show");
        phone.val('');
        var address = modal.find("#shopAddress");
        address.val('');
        var lat = modal.find('#shop-address-lat');
        lat.val('');
        var lng = modal.find('#shop-address-lng');
        lng.val('');
        //商家描述
        var shopDesc = modal.find('#shopDesc');
        shopDesc.val('');
        modal.find("#shop-img-show").html(`<div>图片预览</div>`);
        var businessId = btnThis.data('id')
        var shopInfo = '';
        for(var i in homemakingList){
            if(homemakingList[i].id == businessId){
                shopInfo = homemakingList[i]
                break;
            }
        }
        console.log(shopInfo)
        shopName.val(shopInfo.shop_name);
        account.val(shopInfo.user_name);
        rebate.val(Number(shopInfo.qu_lv*100))
        rebate2.val(Number(shopInfo.shi_lv*100))
        rebate3.val(Number(shopInfo.als_lv*100))
        phone.val(shopInfo.shop_phone);
        address.val(shopInfo.address);
        modal.find('#shop-img-show').html(`<img src="${shopInfo.shop_img}" />`);
        shopDesc.val(shopInfo.content);
        city.val(shopInfo.shi)
    })
    
    $('#BusinessResetModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        modal.find('#businessId').val('')
        var account = modal.find('#account-reset');
        account.val('');
        var password = modal.find('#password-reset');
        password.val('');

        var businessId = btnThis.data('id')
        modal.find('#businessId').val(businessId)
        var shopInfo = '';
        for(var i in homemakingList){
            if(homemakingList[i].id == businessId){
                shopInfo = homemakingList[i]
                break;
            }
        }
        account.val(shopInfo.user_name)
    })
    /** 添加 修改 **/
    $('#addShop').on('click',function(){
        //获取账号
        var account = $('#account').val();
        //获取密码
        var password = $('#password').val(); 
        var rebate = $('#rebate').val()?Number($('#rebate').val()/100):0;
        var rebate2 = $('#rebate2').val()?Number($('#rebate2').val()/100):0;
        var rebate3 = $('#rebate3').val()?Number($('#rebate3').val()/100):0;
        
        //获取名称
        var shopName = $('#shopName').val();
        //获取名称
        var shopPhone = $('#shopPhone').val();
        //数据
        if(account == ''||password == ''|| shopName == '' || shopPhone == ''|| rebate == ''){
            alert('请将信息填写完整！')
        }else{
            var data = {
                "shop_name": shopName,
                "qu_lv": rebate,
                "shi_lv": rebate2,
                "als_lv": rebate3,
                "shop_phone": shopPhone,
                "community_id": global.community_id,
            }
            var data2 = {
                "user_name": account,
                "user_pwd": password,
            }
            data = JSON.stringify(data);
            data2 = JSON.stringify(data2);
            // console.log(data);
            // check_class = JSON.stringify(check_class);
            $.ajax({
                url:`${global.apiUrl}/admin_add_community_health_shop`,
                type:'POST',
                data:{
                    content: data,
                    user_content: data2,
                    phone: shopPhone,
                },
                success(res){
                    // console.log(res);
                    if(res == 1){
                        alert('新的商家，添加成功！');
                        $('#BusinessModal').modal('hide');
                        getHomemakingBusiness(homemakingPageNo,showNum);
                    }else if(res == 0){
                        alert('添加用户失败！请重试');
                    }else if(res == 2){
                        alert('用户名已存在！');
                    }else if(res == 3){
                        alert('添加商户失败！请重试');
                    }else if(res == 4){
                        alert('该手机号不存在！请重试');
                    }
                },
                error(res){
                    console.log(res);
                }
            })

        }
    })

    $('#resetShop').on('click',function(){
        var password = $('#password-reset').val();      
        if(password == ''){
            alert('请填写修改密码');
            return;
        }
        var id = $('#businessId').val()
        data = {
            "id": id,
            "user_password": password,
        }
        data = JSON.stringify(data);
        $.ajax({
            url:`${global.apiUrl}/admin_save_community_health_shop`,
            type:'POST',
            data:{
                pro:data,
            },
            success(res){
                // console.log(res)
                if(res == 1){
                    alert('商铺密码，修改成功！');
                    $('#BusinessResetModal').modal('hide');
                }
            },
            error(res){
                // console.log(res);
            }
        })
    })
    
    //查看订单
    $('#homemaking-business').on('click','.orders',function(){
        var shopId = $(this).data('id');
        var shopName = $(this).data('name')
        $('#shopTxt').text(shopName + '订单')
        $('#ordersListTable').css('display','block')
        getOrdersList(ordersListPageNo,showNum,shopId)
    })


    /*** 商家订单 ***/
    var ordersListPageNo = 1,showNum=10;
    var ordersList = ''
    function getOrdersList(p,showNum,shopId){
        getAjax(`${global.apiUrl}/admin_get_user_community_shop_product_order`,function(res){
            // console.log(res);
            if(res.list.length == 0){
                $("#page3").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getOrdersList(num,showNum,shopId)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="7" align="center">暂无订单</td>
                        </tr>`
                $('#ordersList').html(html);
            }else{
                $("#page3").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getOrdersList(num,showNum,shopId)
                        ordersListPageNo = num
                    }
                });
                var html = '';
                ordersList = res.list
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].name}</td>
                            <td>${list[i].phone}</td>
                            <td>${list[i].all_order_num}</td>
                            <td>${setTime2(list[i].createtime)}</td>
                            <td>${list[i].all_price}</td>
                            <td>${getStatus(list[i].status)}</td>
                            <td class="control-td themeColor">
                                <span data-id="${list[i].id}" data-target="#OrdersModal" data-toggle="modal">查看详情</span> 
                            </td>
                        </tr>
                `
                }
                $('#ordersList').html(html);
            }
        },{
            p:p,
            num:showNum,
            shop_id: shopId,
            status: 10
        })
    }

    $('#OrdersModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var ordersId = btnThis.data('id');
        var ordersInfo = '';
        for(var i in ordersList){
            if(ordersList[i].id == ordersId){
                ordersInfo = ordersList[i]
            }
        }
        var html = ''
        var number = 0
        if(ordersInfo.content.length!=0){
            for(var j in ordersInfo.content){
                number += Number(ordersInfo.content[j].num)
                html += `<div class="shopItemAre">
                            <img src="${ordersInfo.content[j].head_img}" class="shopImg"></img>
                            <div class="shopItem">
                                <div>${ordersInfo.content[j].product_name}</div>
                                <div>${'x'+ ordersInfo.content[j].num}</div>
                            </div>
                        </div>`
            }
            $(".shopMainAre").html(html)
        }
        $('.number').text(number)
        $('.price').text(ordersInfo.all_price)
        $('.orderNum').text(ordersInfo.all_order_num)
        $('.orderDate').text(setTime2(ordersInfo.createtime))
        $('.orderStatus').text(getStatus(ordersInfo.status))
        $('.name').text(ordersInfo.name)
        $('.phone').text(ordersInfo.phone)
        $('.address').text(ordersInfo.sheng + ordersInfo.shi + ordersInfo.qu + ordersInfo.address)
    })

    function getStatus(status){
        let statusTxt = ''
        switch (status) {
          case '0':
            statusTxt = '待付款'
            break;
          case '1':
            statusTxt = '待发货'
            break;
          case '2':
            statusTxt = '待收货'
            break;
          case '3':
            statusTxt = '已完成'
            break;
          case '4':
            statusTxt = '已评价'
            break;
        }
        return statusTxt
    }

    /***
     * 健康商城部分 end
     ***/






    /***
     * 健康资料 begin
     ***/

    var imgurl;
    var imgs = [];  //商家展示图片
    var imgArr = imgUploadShow('content-img-select','content-img-show');
    //新图删除
    $('#content-img-show').on('click','.cancle',function(){
        var index = $(this).parent().index();
        imgArr.splice(index,1);
        var contentImgList = document.getElementById('content-img-show');
        // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('new')[0];
        var contentImgItem = document.getElementsByClassName('content-img-item');
        // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])

    })
    //旧图删除
    $('#content-img-show').on('click','.del',function(){
        var index = $(this).parent().index();
        // console.log(index);
        imgs.splice(index,1);
        var contentImgList = document.getElementById('content-img-show');
        // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('old')[0];
        var contentImgItem = document.getElementsByClassName('content-old-img-item');
        // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])
        // console.log(imgs);
    })
    $('#upload-content-img').on('click',function(){
        allImgUpload(imgArr,setContentImgUrl)
    });
    function setContentImgUrl(res){
        if(res.length != 0){
            imgurl = {
                url:res
            } ;
            imgs.push(imgurl)
            $('#upload-content-img').css('display','none');
        }
    }
    
    $('#EducationModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        if(type == '1' || type == '2'){
            // console.log(type)
            modal.find('#upType').val(type);
            modal.find('#educationId').val('');
            var title = modal.find('#title');
            title.val('');
            var author = modal.find('#author');
            author.val('');
            var video = modal.find("#video");
            var videoAre = modal.find("#videoAre");
            video.val('');
            videoAre.css('display','none')
            var switchBtn = modal.find('.switchBtn');
            var educationImgAre = modal.find("#educationImgAre")
            educationImgAre.css('display','block')
            modal.find('#content-img-show .old').html('');
            modal.find('#content-img-show .new').html('');//可能存在bug
            modal.find("#shop-img-show").html(`<div>图片预览</div>`);
            var summernote = modal.find("#summernote")
            summernote.css('display','block')
            switchBtn.css('border','1px solid #b4b4b4')
            switchBtn.children('.round').css('background','#b4b4b4')
            switchBtn.eq(0).css('border','1px solid #0b67ff');
            switchBtn.eq(0).children('.round').css('background','#0b67ff');
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                onImageUpload: function(files, editor, $editable) {
                    sendFile(files[0],editor,$editable);
                }
            })
            $('.summernote').code('');
            $('#upload-content-img').css('display','none');
            //根据type判断是2修改还是1新增
            if(type == 1){
                modal.find('#educationTxt').text('添加内容');
            }
            else if(type == 2){
                modal.find('#educationTxt').text('修改内容');
                var educationId = btnThis.data('id')
                modal.find('#educationId').val(educationId);
                var educationInfo = '';
                for(var i in educationList){
                    if(educationList[i].id == educationId){
                        educationInfo = educationList[i]
                        break;
                    }
                }
                console.log(educationInfo)
                t = educationInfo.type                        
                title.val(educationInfo.name);
                author.val(educationInfo.up_name);
                if(t == 1){
                    //文章
                    $("#videoAre").css('display','none')
                    $("#educationImgAre").css('display','block')
                    $("#summernote").css('display','block')
                    $('.summernote').code(educationInfo.content);
                    
                    imgs = educationInfo.head_img;
                    var head_img = educationInfo.head_img;
                    // console.log(head_banner);
                    var html = '';
                    for(var j in head_img){
                        html+= `
                            <div class="content-old-img-item">
                                <img src="${head_img[j].url}" alt="">
                                <div class="del themeColor">删 除</div>
                            </div>
                        `
                    }
                    modal.find('#content-img-show  .old').html(html);
                }else if(t == 2){
                    //视频
                    switchBtn.css('border','1px solid #b4b4b4')
                    switchBtn.children('.round').css('background','#b4b4b4')
                    switchBtn.eq(1).css('border','1px solid #0b67ff');
                    switchBtn.eq(1).children('.round').css('background','#0b67ff');
                    $("#videoAre").css('display','block')
                    $("#educationImgAre").css('display','none')
                    $("#summernote").css('display','none')
                    video.val(educationInfo.url)
                }
            }
        }
        
    })

    
    //切换按钮
    var t = 1
    $(".switchBtn").on('click',function(){
        var type = $(this).data('type')
        // console.log(type)
        if(type == 'article'){
            //文章
            $("#videoAre").css('display','none')
            $("#educationImgAre").css('display','block')
            $("#summernote").css('display','block')
            t = 1
            
        }else if(type == 'video'){
            //视频
            $("#videoAre").css('display','block')
            $("#educationImgAre").css('display','none')
            $("#summernote").css('display','none')
            t = 2
        }
        
        $('.switchBtn').css('border','1px solid #b4b4b4')
        $('.switchBtn').children('.round').css('background','#b4b4b4')
        $(this).css('border','1px solid #0b67ff')
        $(this).children('.round').css('background','#0b67ff')
    })

    //提交按钮
    $("#educationBtn").on('click',function(){
        var title = $("#title").val()
        var author = $("#author").val()
        if(title == ''||author == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#upType").val();
        // console.log('type:',type);
        if(type == 1){
            if(t == 1){
                var data = {}
                //文章
                var content = $('.summernote').code()
                if(imgs.length == 0){
                    alert('请上传图片！')
                    return
                }
                if(!content){
                    alert('请填写视频文章内容！')
                    return
                }
                data = {
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "content": content,
                    "community_id": global.community_id,
                    "head_img": imgs
                }
            }else if(t == 2){
                //视频
                var video = $('#video').val()
                console.log(video)
                if(!video){
                    alert('请填写视频路径！')
                    return
                }
                data = {
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "url": video,
                    "community_id": global.community_id,
                }
            }
        }else if(type == 2){
            var id = $("#educationId").val();
            if(t == 1){
                //文章
                var content = $('.summernote').code()
                // console.log(imgs)
                if(imgs.length == 0){
                    alert('请上传图片！')
                    return
                }
                if(!content){
                    alert('请填写视频文章内容！')
                    return
                }
                data = {
                    "id": id,
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "content": content,
                    "community_id": global.community_id,
                    "head_img": imgs
                }
            }else if(t == 2){
                //视频
                var video = $('#video').val()
                if(!video){
                    alert('请填写视频路径！')
                    return
                }
                data = {
                    "id": id,
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "url": video,
                    "community_id": global.community_id,
                }
            }
        }
        // console.log(content)
        data = JSON.stringify(data);
        // console.log(data);
        var text = '';
        var text2 = t==1?'文章':'视频'
        if(type == 2){
            text = text2 + '信息，修改成功！';
        }else if(type == 1){
            text = '新的'+ text2 +'，添加成功！';
        }
        postAjax(`${global.apiUrl}/admin_add_community_health_new_video`,function(res){
            if(res == 1){
                alert(text);
                $('#EducationModal').modal('hide');
                getEducationList(educationPageNo,showNum)                
            }
        },{
            type:type,
            t: t,
            pro: data
        })
    })

    //获取文章视频列表
    var educationPageNo = 1,showNum=10;
    var educationList = '';
    getEducationList(educationPageNo,showNum)
    
    function getEducationList(p,showNum){
        var data = []
        data.push(global.community_id)
        data = JSON.stringify(data)
        getAjax(`${global.apiUrl}/get_community_health_new_video_all`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page2").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                    getEducationList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无文章视频</td>
                        </tr>`
                $('#Education-List').html(html);
            }else{
                $("#page2").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                    getEducationList(num,showNum)
                    educationPageNo = num
                    }
                });
                var html = '';
                educationList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].createtime}</td>
                        <td>${list[i].up_name}</td>
                        <td>${type(list[i].type)}</td>
                        <td class="control-td themeColor">
                            <span class="education-reset" data-id="${list[i].id}" data-type="2" data-target="#EducationModal" data-toggle="modal">修改</span> 
                            <span class="education-delete" style="margin-left: 15px" data-id="${list[i].id}" data-type="${list[i].type}">删除</span>
                            ${select(list[i].status,list[i].id,list[i].type)}
                        </td>
                        </tr>
                `
                }
                $('#Education-List').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id: data
        })
    }
    
    function type(type){
        var text = ''
        switch (type) {
            case '1':
                text = '文章'
                break;
            case '2':
                text = '视频'
                break;
        }
        return text
    }
     
    function select(status,id,type){
        var html = ''
        switch (status) {
            case '0':
                html = `<span class="education-select" style="margin-left: 15px" data-status="${status}" data-id="${id}" data-type="${type}">设为精选</span>`
                break;
            case '1':
                html = `<span class="education-select" style="margin-left: 15px" data-status="${status}" data-id="${id}" data-type="${type}">取消精选</span>`
                break;
        }
        return html
    }

    /** 删除 **/
    $('#Education-List').on('click','.education-delete',function(){
        var educationId = $(this).data('id');
        var type = $(this).data('type')
        var text = type==1?'文章':'视频'
        var f = confirm('是否删除该' + text);
        if(f){
            getAjax(`${global.apiUrl}/admin_add_community_health_new_video`,function(res){
                // console.log(res);
                if(res == 1){
                    alert(text + '删除成功！');
                    getEducationList(educationPageNo,showNum);
                }
            },{
                t: type,
                type: 3,
                id: educationId
            })
        }
    })

    /** 设为精选 **/
    $('#Education-List').on('click','.education-select',function(){
        var status = $(this).data('status');
        var educationId = $(this).data('id');
        var type = $(this).data('type')
        var text = type==1?'文章':'视频'
        var set = status== 0?'1':'0'
        var text2 = set==0?'取消':'设为'
        var f = confirm('是否该' + text + text2 + '精选');
        if(f){
            getAjax(`${global.apiUrl}/admin_add_community_health_new_video`,function(res){
                // console.log(res);
                if(res == 1){
                    alert(text + text2 + '精选成功！');
                    getEducationList(educationPageNo,showNum);
                }
            },{
                t: type,
                type: 4,
                id: educationId,
                status: set
            })
        }
    })

    /***
     * 健康资料 end
     ***/



    


    /***
     * 健康讲堂 begin
     ***/

    $('#lectureModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#lectureUpType').val(type);
        modal.find('#lectureId').val('');
        var title = modal.find('#title2');
        title.val('');
        var author = modal.find('#author2');
        author.val('');
        var video = modal.find("#video2");
        video.val('');
        //根据type判断是2修改还是1新增
        if(type == 1){
            modal.find('#lectureTxt').text('添加内容');
        }
        else if(type == 2){
            modal.find('#lectureTxt').text('修改内容');
            var lectureId = btnThis.data('id')
            modal.find('#lectureId').val(lectureId);
            var lectureInfo = '';
            for(var i in lectureList){
                if(lectureList[i].id == lectureId){
                    lectureInfo = lectureList[i]
                    break;
                }
            }
            // console.log(lectureInfo)
            t = lectureInfo.type                        
            title.val(lectureInfo.name);
            author.val(lectureInfo.up_name);
            video.val(lectureInfo.url)
        }
    })


    //提交按钮
    $("#lectureBtn").on('click',function(){
        var title = $("#title2").val()
        var author = $("#author2").val()
        var video = $('#video2').val()
        if(title == ''||author == '' || video == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#lectureUpType").val();
        // console.log('type:',type);
        if(type == 1){
            data = {
                "name": title,
                "up_name": author,
                "status": 0,
                "see": 0,
                "createtime": setTime(new Date()),
                "url": video,
                "community_id": global.community_id,
            }
        }else if(type == 2){
            var id = $("#lectureId").val();
            data = {
                "id": id,
                "name": title,
                "up_name": author,
                "status": 0,
                "see": 0,
                "createtime": setTime(new Date()),
                "url": video,
                "community_id": global.community_id,
            }
        }
        // console.log(content)
        data = JSON.stringify(data);
        console.log(type)
        console.log(data);
        var text = '';
        if(type == 2){
            text = '视频信息，修改成功！';
        }else if(type == 1){
            text = '新的视频，添加成功！';
        }
        postAjax(`${global.apiUrl}/Api/Communityhealth/operation_community_health_teacher_video`,function(res){
            console.log(res)
            if(res == 1){
                alert(text);
                $('#lectureModal').modal('hide');
                getLectureList(lecturePageNo,showNum)                
            }
        },{
            type:type,
            pro: data
        })
    })

    //获取文章视频列表
    var lecturePageNo = 1,showNum=10;
    var lectureList = '';
    getLectureList(lecturePageNo,showNum)
    function getLectureList(p,showNum){
        var data = []
        data.push(global.community_id)
        data = JSON.stringify(data)
        console.log(data)
        getAjax(`${global.apiUrl}/get_community_health_teacher_video`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page4").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getLectureList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无健康讲堂视频</td>
                        </tr>`
                $('#lecture-List').html(html);
            }else{
                $("#page4").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getLectureList(num,showNum)
                        lecturePageNo = num
                    }
                });
                var html = '';
                lectureList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].name}</td>
                            <td>${list[i].createtime}</td>
                            <td>${list[i].up_name}</td>
                            <td class="control-td themeColor">
                                <span class="lecture-reset" data-id="${list[i].id}" data-type="2" data-target="#lectureModal" data-toggle="modal">修改</span> 
                                <span class="lecture-delete" style="margin-left: 15px" data-id="${list[i].id}" >删除</span>
                            </td>
                        </tr>
                `
                }
                $('#lecture-List').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id: data
        })
    }

    /** 删除 **/
    $('#lecture-List').on('click','.lecture-delete',function(){
        var lectureId = $(this).data('id');
        var f = confirm('是否删除该视频');
        if(f){
            getAjax(`${global.apiUrl}/Api/Communityhealth/operation_community_health_teacher_video`,function(res){
                // console.log(res);
                if(res == 1){
                    alert('该视频删除成功！');
                    getLectureList(lecturePageNo,showNum);
                }
            },{
                type: 3,
                id: lectureId
            })
        }
    })

    /***
     * 健康讲堂 end
     ***/


    /***
    *  健康咨询部分 begin
    ***/

    /*** banner ***/
    var consultBannerList = ''
    //健康商城服务banner
    function getConsultBannerList(){
        getAjax(`${global.apiUrl}/get_community_health_expert_banner`,function(res){
            console.log(res);
            var html = '';
            consultBannerList = res;
            for(var i in res){
                html += `
                 <div class="banner-item">
                    <img src="${res[i].banner_img}" alt="" >
                    <div class="eventBox themeColor">
                        <div class="reset" data-toggle="modal" data-target="#consultBannerModal" data-id="${res[i].id}" data-type="2">修改</div>
                        <div class="delete" data-id="${res[i].id}" >删除</div>
                    </div>
                 </div>
            `
            }
            html += `
            <div class="banner-add" data-toggle="modal" data-target="#consultBannerModal" data-type="1">
                <img src="../../img/community-service/banner_add.png" alt="">
            </div>
        `
            $('#banner-consult-item-box').html(html);
        },{
            community_id: global.community_id
        })
    }
    getConsultBannerList();
    $('#banner-consult-item-box').on('mouseenter','.banner-item',function(){
        $(this).find('.eventBox').css('display','block');
        $(this).find('.eventBox').css('opacity','1');
    })
    $('#banner-consult-item-box').on('mouseleave','.banner-item',function(){
        $(this).find('.eventBox').css('opacity','.3');
        $(this).find('.eventBox').css('display','none');
    })
    //modal 触发同时
    $('#consultBannerModal').on('show.bs.modal',function(event){
        var btnThis = $(event.relatedTarget);
        var modal = $(this);
        // modal.find('.preview-img')
        modal.find('#consultImgUrl').val('');
        modal.find('#consultImage').html(`<div>图片预览</div>`);
        var type = btnThis.data('type');
        // console.log(type);
        if(type == 1){
            modal.find('#consultModalLabel').text('添加BANNER');
            modal.find('#uploadConsultBannerImg').attr('data-type','1');
            modal.find('#consultBannerId').val('');
        }
        else if(type == 2){
            modal.find('#consultModalLabel').text('修改BANNER');
            modal.find('#uploadConsultBannerImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id,mainBanner);
            modal.find('#consultBannerId').val(id);
            // console.log(homemakingBannerList);
            for(var i in consultBannerList){
                if(id == consultBannerList[i].id){
                    modal.find('#consultImage').html(`<img src="${consultBannerList[i].banner_img}" >`)
                }
            }
        }
    });
    function setConsultImgUrl(res){
        // console.log(res)
        document.getElementById('consultImgUrl').value = res;
    }
    imgUpload('consultInput','consultImage',setConsultImgUrl);

    //上传，修改
    $('#uploadConsultBannerImg').on('click',function(){
        var $imgUrl = $('#consultImgUrl').val();
        var $bannerId = $('#consultBannerId').val();
        var type = $('#uploadConsultBannerImg').data('type');
        // console.log($imgUrl,$bannerId);
        // console.log(type);
        if($imgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            getAjax(`${global.apiUrl}/admin_operation_community_health_expert_banner`,function(res){
                if(res == 1){
                    getConsultBannerList();
                    alert('上传成功！');
                    $('#consultBannerModal').modal('hide');
                }
            },{
                type:type,
                id:$bannerId,
                banner_img:$imgUrl,
                community_id: global.community_id
            })
        }
    })
    //删除
    $('#banner-consult-item-box').on('click','.delete',function(){
        var id = $(this).data('id');
        var r = confirm("是否要删除该轮播图？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_health_expert_banner`,function(res){
                if(res == 1){
                    getConsultBannerList();
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }

    })

    /***  分类 begin   ***/
    var consultClass = '';
    function getConsultClass(){
        getAjax(`${global.apiUrl}/get_community_health_expert_class`,function(res){
            console.log(res);
            var html = '';
            var addConsultClass = '';
            var selHtml = '<option value="0">全部</option>';
            consultClass = res;
            for(var i in res){
                html += `
                    <div class="class-item">
                    <div class="icon-box">
                        <img src="../../img/community-service/shop.png" alt="">
                    </div>
                    <div class="icon-text">${res[i].class_name}</div>
                    <div class="class-reset themeColor" data-toggle="modal" data-target="#consultClassModal" data-type="2" data-id="${res[i].id}">修改</div>
                    <div class="class-delete themeColor" data-id="${res[i].id}">删除</div>
                </div>
                `
                selHtml += `
                     <option value="${res[i].id}">${res[i].class_name}</option>
                     
                `
                addConsultClass += `
                    <div class="checkbox">
                        <label>
                            <input type="radio" class="consult-Class-checked" name="class" value="${res[i].id}"> ${res[i].class_name}
                        </label>
                    </div>
                `
            }
            html += `
                <div class="class-item-add" data-toggle="modal" data-target="#consultClassModal" data-type="1">
                    <div class="icon-box-add">
                        <img src="../../img/community-service/banner_add.png" alt="">
                    </div>
                    <div class="icon-text-add">添加</div>
                </div>
            `
            $('#consultClass').html(html);
            $('#consult-class-sel').html(selHtml);
            $('#add-consult-class-box').html(addConsultClass);

        },{
            community_id: global.community_id
        })
    }
    getConsultClass();
    $('#consultClass').on('mouseenter','.class-item',function(){
        // console.log(1);
        $(this).css('boxShadow','0 0 8px #f1f1f1');
        $(this).find('.class-reset').css('display','block');
        $(this).find('.class-delete').css('display','block');
    })
    $('#consultClass').on('mouseleave','.class-item',function(){
        $(this).css('boxShadow','');
        $(this).find('.class-reset').css('display','none');
        $(this).find('.class-delete').css('display','none');
    })
    $('#consultClassModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        // console.log(type);
        modal.find('#consultClassName').val('');
        // modal.find('.preview-class-img').html(`<div>图片预览</div>`);
        if(type == 1){
            modal.find('#consultClassModalLabel').text('添加分类');
            modal.find('#uploadConsultClassImg').attr('data-type','1');
            modal.find('#consultClassItemId').val('');
        }else if(type == 2){
            modal.find('#consultClassModalLabel').text('修改分类');
            modal.find('#uploadConsultClassImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id);
            // console.log(id,mainBanner);
            modal.find('#consultClassItemId').val(id);
            // console.log(homemakingClass);
            for(var i in consultClass){
                if(id == consultClass[i].id){
                    // modal.find('.preview-class-img').html(`<img src="${consultClass[i].url}" >`);
                    modal.find('#consultClassName').val(consultClass[i].class_name);
                }
            }
        }
    })
    // function setClassImgUrl(res){
    //     document.getElementById('classImgUrl').value = res;
    // }
    //上传图片获取图片地址
    // imgUpload('classInput','classImage',setClassImgUrl);
    $('#uploadConsultClassImg').on('click',function(){
        // var $classImgUrl = $('#classImgUrl').val();
        var $className = $('#consultClassName').val();
        var $classId = $('#consultClassItemId').val();
        var type = $('#uploadConsultClassImg').data('type');
        // console.log($classImgUrl,$classId,$className);
        // console.log(type);
        getAjax(`${global.apiUrl}/admin_operation_community_health_expert_class`,function(res){
            if(res == 1){
                getConsultClass();
                alert('上传成功！');
                $('#consultClassModal').modal('hide');
            }
        },{
            type:type,
            id:$classId,
            class_name:$className,
            community_id: global.community_id
        })
    })
    $('#consultClass').on('click','.class-delete',function(){
        var id = $(this).data('id');
        // console.log(id);
        var f = confirm('删除分类可能造成！软件异常，是否确实删除');
        if(f){
            getAjax(`${global.apiUrl}/admin_operation_community_health_expert_class`,function(res){
                // console.log(res);
                if(res == 1){
                   getConsultClass();
                //    console.log(res);
                   alert('删除成功！')
               }else if(res == 2){
                   alert('该分类存在健康管理师！不可删除！');
               }

            },{
                type:3,
                id:id
            })
        }
    })


    /***  门店电话  ***/
    function getCommunityPhone(){
        //type 1新增 2修改 3查看
        getAjax(`${global.apiUrl}/admin_operation_community_address_phone`,function(res){
            // console.log(res)
            if(res.address_phone){
                communityPhoneType = 2
                $('#shopMainPhone').val(res.address_phone)
                $('#phoneBtn').css('display','none')
            }else{
                communityPhoneType = 1
            }
        },{type: 3,id: global.community_id})
    }
    getCommunityPhone()

    $("#shopMainPhone").bind("input propertychange",function () {
        $('#phoneBtn').css('display','block')
    });

    $('#phoneBtn').on('click',function(){
        var phone = $('#shopMainPhone').val();
        if(phone != ''){
            getAjax(`${global.apiUrl}/admin_operation_community_address_phone`,function(res){
                console.log(res)
                if(res == 1){
                    alert('门店电话设置成功')
                }
            },{type: 2,address_phone: phone,id: global.community_id})
        }
    })

    /***  健康管理师列表  ***/
    //获取健康管理师列表
    var consultPageNo = 1,showNum=10;
    var consultList = '';
    var consultLike = '';
    var consultClassId = '';
    function getConsultList(p,showNum,classId,like){
        getAjax(`${global.apiUrl}/admin_community_get_health_expert`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page3").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getConsultList(num,showNum,classId,like)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="7" align="center">暂无健康管理师</td>
                        </tr>`
                $('#consultList').html(html);
            }else{
                $("#page3").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.all,
                    callback: function(num) {
                        getConsultList(num,showNum,classId,like)
                        consultPageNo = num
                    }
                });
                var html = '';
                consultList = res.list;
                var list = res.list;
                var className = '';
                for(var i in list){
                    
                    html += `
                        <tr>
                            <td class="shop-img">
                                <img src="${list[i].img}" alt="">
                            </td>
                            <td>${list[i].name}</td>
                            <td>${list[i].phone}</td>                            
                            <td>${type2(list[i].class_id)}</td>
                            <td>${list[i].yes_ok}</td>
                            <td>${list[i].sub_content}</td>
                            <td class="control-td themeColor">
                                <span class="reset" data-toggle="modal" data-target="#consultModal" data-id="${list[i].id}" data-type="2">修改</span>
                                <span class="delete" data-id="${list[i].id}" style="margin-left: 15px">删除</span>
                                ${select2(list[i].status,list[i].id)}
                            </td>
                        </tr>
                `
                }
                $('#consultList').html(html);
            }
        },{
            p:p,
            num:showNum,
            class_id: classId,
            like: like,
            community_id:global.community_id
        })
    }
    getConsultList(consultPageNo,showNum);
    
    function type2(classId){
        var className = ''
        for(var i in consultClass){
            if(consultClass[i].id == classId){
                className = consultClass[i].class_name
                break
            }
        }
        return className
    }

    function select2(status,id){
        var html = ''
        switch (status) {
            case '0':
                html = `<span class="consult-select" style="margin-left: 15px" data-status="${status}" data-id="${id}">设为推荐</span>`
                break;
            case '1':
                html = `<span class="consult-select" style="margin-left: 15px" data-status="${status}" data-id="${id}">取消推荐</span>`
                break;
        }
        return html
    }

    //服务类别筛选
    $('#consult-class-sel').on('change',function(){
        var classId = $(this).children('option:selected').val()
        console.log(classId);
        consultClassId = classId;
        getConsultList(consultPageNo,showNum,consultClassId,consultLike);
    });
    $('#consult-search').on('click',function(){
        consultLike = $('.search-box').val();
        getConsultList(homemakingPageNo,showNum,consultClassId,consultLike)
    })

    /** 添加商户 **/
    function setConsultShopImgUrl(res){
        document.getElementById('consult-img-url').value = res;
    }
    imgUpload('consult-img-select','consult-img-show',setConsultShopImgUrl);


    $('#consultModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#consultUpType').val(type);
        // modal.find('#upload-content-img').css('display','block');
        modal.find('#consultId').val('');
        
        var consultName = modal.find('#consultName');
        consultName.val('');
        var phone = modal.find("#consultPhone");
        phone.val('');
        var consultImg = modal.find('#consult-img-url');
        consultImg.val('');
        //商家描述
        var consultDesc = modal.find('#consultDesc');
        consultDesc.val('');
        var consultField = modal.find('#consultField');
        consultField.val('');
        
        modal.find("#consult-img-show").html(`<div>图片预览</div>`);

        //分类
        var obj = $(".consult-Class-checked");
        obj.attr('checked',false);
        //根据type判断是2修改还是1新增
        if(type == 1){
            modal.find('#consultModalLabel').text('新增健康管理师');
        }else if(type == 2){
            modal.find('#consultModalLabel').text('修改健康管理师');
            var consultId = btnThis.data('id')
            modal.find('#consultId').val(consultId);

            var expertsInfo = '';
            for(var i in consultList){
                if(consultList[i].id == consultId){
                    expertsInfo = consultList[i]
                    break;
                }
            }
            // console.log(expertsInfo)
            consultName.val(expertsInfo.name)
            phone.val(expertsInfo.phone)
            consultImg.val(expertsInfo.img)
            modal.find('#consult-img-show').html(`<img src="${expertsInfo.img}"/>`);
            consultDesc.val(expertsInfo.sub_content)
            consultField.val(expertsInfo.yes_ok)
            obj.each(function(){
                var that = $(this);
                if(that.val() == expertsInfo.class_id){
                    // console.log(that.val());
                    that.prop('checked',true);
                }
            })
        }

    })

    /** 添加 修改 **/
    $('#addConsult').on('click',function(){
        var consultType = $("#consultUpType").val();
        //获取选择的类别
        var obj = document.getElementsByClassName("consult-Class-checked");
        var check_class = '';
        for(k in obj){
            if(obj[k].checked){
                check_class = obj[k].value;
            }
        }
        var consultName = $('#consultName').val();
        var phone = $('#consultPhone').val();
        var consultImg = $('#consult-img-url').val();
        var consultDesc = $('#consultDesc').val();
        var consultField = $('#consultField').val();
        // console.log(consultType)
        //数据
        if(consultName == ''||phone == ''||consultImg == ''||consultDesc == ''||consultField == ''|| check_class == ''){
            alert('请将信息填写完整！')
        }else{
            var data = {}
            if(consultType == 1){
                //新增
                data = {
                    "name": consultName,
                    "img" : consultImg,
                    "sub_content": consultDesc,
                    "yes_ok": consultField,
                    "phone" : phone,
                    "class_id": check_class,
                    "community_id": global.community_id
                }
            }else if(consultType == 2){
                //修改
                var id = $("#consultId").val();
                data = {
                    "id": id,
                    "name": consultName,
                    "img" : consultImg,
                    "sub_content": consultDesc,
                    "yes_ok": consultField,
                    "phone" : phone,
                    "class_id": check_class,
                    "community_id": global.community_id
                }
            }
            // console.log(data)
            data = JSON.stringify(data);
            var text = ''
            if(consultType == 2){
                text = '健康管理师信息，修改成功！';
            }else if(consultType == 1){
                text = '新的健康管理师，添加成功！';
            }
            console.log(data)
            $.ajax({
                url:`${global.apiUrl}/admin_community_operation_health_expert`,
                type:'POST',
                data:{
                    type: consultType,
                    pro: data,
                },
                success(res){
                    // console.log(res);
                    if(res == 1){
                        alert(text);
                        $('#consultModal').modal('hide');
                        getConsultList(consultPageNo,showNum,consultClassId,consultLike);
                    }else if(res == 4){
                        alert('该管理师手机号不存在！请重试');
                    }else if(res == 0){
                        alert('操作失败！请重试');
                    }
                },
                error(res){
                    console.log(res);
                }
            })
        }
    })

    /** 设为精选 **/
    $('#consultList').on('click','.consult-select',function(){
        var status = $(this).data('status');
        var consultId = $(this).data('id');
        var set = status== 0?'1':'0'
        var text = set==0?'取消':'设为'
        var f = confirm('是否该健康管理师' + text + '推荐');
        var data = {
            "id": consultId,
            "status": set
        }
        data = JSON.stringify(data);
        if(f){
            getAjax(`${global.apiUrl}/admin_community_operation_health_expert`,function(res){
                // console.log(res);
                if(res == 1){
                    alert(text  + '推荐成功！');
                    getConsultList(consultPageNo,showNum,consultClassId,consultLike);
                }
            },{
                type: 2,
                pro: data
            })
        }
    })

    
    
    /** 删除 **/
    $('#consultList').on('click','.delete',function(){
        var consultId = $(this).data('id');
        var f = confirm('是否删除该健康管理师');
        if(f){
            getAjax(`${global.apiUrl}/admin_community_operation_health_expert`,function(res){
                // console.log(res);
                if(res == 1){
                    alert('健康管理师删除成功！');
                    getConsultList(consultPageNo,showNum,consultClassId,consultLike);
                }
            },{
                type: 3,
                id:consultId
            })
        }

    })

    /***
     * 健康咨询部分 end
     ***/
});
